<?php
/**
 * @title 63. 不同路径 II
 * @author start2004
 */

// ini_set("memory_limit", "128M");

// class Solution {
//
//     function find() {
//
//     }
// }


class Solution {

    /**
     * @param Integer[][] $obstacleGrid
     * @return Integer
     */
    function uniquePathsWithObstacles($obstacleGrid) {
        /**
         * @since 2024-01-02 行数和列数
         */
        $row = count($obstacleGrid);
        $col = count($obstacleGrid[0]);

        /**
         * @since 2024-01-02 起点和终点有障碍物
         */
        if($obstacleGrid[0][0] == 1 OR $obstacleGrid[$row-1][$col-1] == 1) {
            return 0;
        } else {}

        /**
         * @since 2024-01-02 新建一个到每个点的路径条数
         */
        $dp = array_fill(0, $row, array_fill(0, $col, 0));
        $dp[0][0] = 1;

        /**
         * @since 2024-01-02 从[0,0]出发，广度遍历
         */
        $nodeArray = [[0,0]];
        while($nodeArray){
            /**
             * @since 2024-01-02 下次遍历的点集合
             */
            $nextNodeArray = [];

            /**
             * @since 2024-01-02 遍历当前点
             */
            foreach ($nodeArray as list($r, $c)){
                /**
                 * @since 2024-01-02 每次只能向下或者向右移动一步
                 */
                $moveArray = [
                    [$r+1, $c],
                    [$r, $c+1],
                    // [$r-1, $c],
                    // [$r, $c-1],
                ];

                /**
                 * @since 2024-01-02 遍历
                 */
                foreach ($moveArray as list($nextR, $nextC)){
                    /**
                     * @since 2024-01-02 矩阵范围内，且非障碍物
                     */
                    if(isset($obstacleGrid[$nextR][$nextC]) AND $obstacleGrid[$nextR][$nextC] == 0){
                        /**
                         * @since 2024-01-02 累计路径条数
                         */
                        $dp[$nextR][$nextC] += $dp[$r][$c];

                        /**
                         * @since 2024-01-02 下次遍历的点，key=row-col，避免重复
                         */
                        $nextNodeArray["{$nextR}-{$nextC}"] = [$nextR, $nextC];
                    } else {}
                }
            }

            /**
             * @since 2024-01-02 下次遍历的点集合
             */
            $nodeArray = $nextNodeArray;
        }

        /**
         * @return
         */
        return $dp[$row-1][$col-1];
    }
}



/**
 * @url http://127.0.0.1/leetcode/202401/2024.01.02_6.php
 */
$datas = [
    [[[0,0,0],[0,1,0],[0,0,0]]], // 2
];

include_once dirname(__DIR__) . DIRECTORY_SEPARATOR ."xhprof.php";
$xhprof = new Xhprof();
foreach ($datas as $data){
    if(count($data) <= 20){
        var_dump($data);
    } else {
        var_dump(count($data));
    }

    $obj = new Solution();
    $result = $obj->uniquePathsWithObstacles(...$data);
    // $result = $obj->($xhprof->tree($data));
    // $result = $obj->($xhprof->listNode($data));
    // $result = $obj->find(...$data);
    var_dump($result);
    // if(count($result)<=20){
    //     var_dump($result);
    // } else {
    //     var_dump(count($result));
    // }
    echo str_repeat(PHP_EOL, 3);
    echo str_repeat("<br>", 3);
}

// foreach ($datas as $data){
//     $obj = new $data[0][0](...$data[1][0]);
//
//     for ($i=1; $i<count($data[0]); $i++){
//         $func = $data[0][$i];
//         $param = $data[1][$i];
//         $result = $obj->$func(...$param);
//
//         echo $data[0][$i] ."(\"". implode(",", $data[1][$i]) ."\") ";
//             if($result === true){
//                 echo "True";
//             } elseif($result === false) {
//                 echo "False";
//             } elseif($result === null) {
//                 echo "Null";
//             } elseif(is_array($result)) {
//                 var_dump($result);
//             } else {
//                 echo $result;
//             }
//         echo PHP_EOL;
//     }
//
//     echo str_repeat(PHP_EOL, 3);
// }
$xhprof->end();
